package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.PageHelper;
import sy.pageModel.SessionInfo;
import sy.model.Ttransport;
import sy.pageModel.Transport;
import sy.service.TransportServiceI;
import sy.util.ConfigUtil;
import sy.util.excel.ExcelUtils;
import sy.util.excel.JsGridReportBase;
import sy.util.excel.TableData;

/**
 * 预约控制器
 * 
 */
@Controller
@RequestMapping("/transportController")
public class TransportController extends BaseController {

	@Autowired
	private TransportServiceI transportService;


	/**
	 * 跳转到预约管理页面
	 *
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "/business/transport";
	}

	/**
	 * 获取预约数据表格
	 *
	 * @param
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(Transport transport, PageHelper ph,SessionInfo sessionInfo) {
		return transportService.dataGrid(transport, ph,sessionInfo);
	}

	/**
	 * 跳转到添加预约页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		Transport transport = new Transport();
		transport.setId(UUID.randomUUID().toString());
		request.setAttribute("transport", transport);
		return "/business/transportAdd";
	}

	/**
	 * 添加预约
	 *
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Transport transport) {
		Json j = new Json();
		try {
			transportService.add(transport);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(transport);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}


	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		transportService.delete(id);
		j.setMsg("删除成功！");
		j.setSuccess(true);
		return j;
	}

	/**
	 * 批量删除用户
	 *
	 * @param ids ('0','1','2')
	 * @return
	 */
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Json batchDelete(String ids) {
		Json j = new Json();
		if (ids != null && ids.length() > 0) {
			for (String id : ids.split(",")) {
				if (id != null) {
					this.delete(id);
				}
			}
		}
		j.setMsg("批量删除成功！");
		j.setSuccess(true);
		return j;
	}

	@RequestMapping("/importExcel")
	public String importExcel() {
		return "/business/excelImport";
	}
	/**
	 * 跳转到trace管理页面
	 *
	 * @return
	 */
	@RequestMapping("/trace")
	public String trace() {
		return "/business/trace";
	}

	//导入
	@RequestMapping("/transportImport")
	@ResponseBody
	synchronized public Json savetransportwithExcelImport(@RequestParam("file") MultipartFile file, HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String name = sessionInfo.getName();
		Json j = new Json();
		String message = transportService.savetransportwithExcel(file, name);
		j.setSuccess(true);
		j.setMsg(message);
		return j;
	}

	@RequestMapping("/tracedataGrid")
	@ResponseBody
	public DataGrid tracedataGrid(Transport transport,PageHelper ph,HttpSession session) {
		SessionInfo sessionInfo1 = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		return transportService.tracedataGrid(transport, ph,sessionInfo1);
	}


}


	
	
    //excel导出
//	@RequestMapping("/exportBespeaks")
//	@ResponseBody
//    public void exportBespeaks(HttpServletRequest request,HttpServletResponse response,Transport transport) throws Exception{
//
//    response.setContentType("application/msexcel;charset=GBK");
//    List<Transport> list =transportService.exporttransportsExcel(transport);//获取数据
//    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//	String formatdate = format.format(new Date());
//    String title = "transport"+formatdate;
//    String[] hearders = new String[] {"ID","PID","金佰利订单号","预约编号","状态","行号", "发货方编码", "收货方编码",
//    									"产品编码","系统数量","系统体积","发运方类型","承运商编码","车牌号","实际数量","实际体积","预约时间","备注","货主编码"};//表头数组
//
//    String[] fields = new String[] {"id","pId","code","bookingCode","status",
//    									"lineNum","senderCode","receiverCode","productCode",
//    									"orderQty","orderVolume","shipperType","carrierCode","plateNumber","transportQty","transportVolume","transportTime","remark","ownerCode"};//拒收单对象属性数组
//    TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders),fields);
//    JsGridReportBase report = new JsGridReportBase(request, response);
//    report.exportToExcel(title, "admin", td);
//    }
//
//}
